package com.test.restruant.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.test.restruant.pojo.DishTable;
import com.test.restruant.service.DishTableService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("table.html")
public class DishTableController {
    @Reference
    private DishTableService dishTableService;
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,
                new CustomDateEditor(dateFormat, true));
    }

    /**
     * layui 页码默认叫page   每页几条叫limit  不能改
     * @param name
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(params = "act=list")
    public  Map<String,Object> list(String name, Date start, Integer page, Integer limit) {
        System.out.println(name);
        System.out.println(start);
        System.out.println(limit);
        Map<String,Object> result=new HashMap<>(3);
        PageInfo pageInfo=dishTableService.getPageList(name,page,limit);
        result.put("code",0);
        //当你要分页的时候，前端需要两个key  count=总条数 data=数据列表
        result.put("count",pageInfo.getTotal());
        result.put("data",pageInfo.getList());
        return result;
    }

    @RequestMapping(params = "act=add")
    public  Map<String,Object> add(DishTable dishTable){
        Map<String,Object> result=new HashMap<>();
        try {
            dishTableService.insert(dishTable);
            result.put("status",true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status",false);
            result.put("message",e.getMessage());
        }
        return result;
    }

    @RequestMapping(params = "act=delete")
    public  Map<String,Object> delete(int[]ids){
        System.out.println(Arrays.toString(ids));
        //删除略
        Map<String,Object> result=new HashMap<>();
        result.put("status",true);
        return result;
    }
}
